package org.ratingviewer.aspects;

import java.text.MessageFormat;

import org.aspectj.lang.JoinPoint;
import org.ratingviewer.resources.Resources;
import org.slf4j.*;

public aspect ExceptionHandler issingleton()
{

	pointcut npcException(Throwable throwable): !cflow(adviceexecution())
			&& withincode(* org.ratingviewer..*.*(..))
			&& !withincode(* org.ratingviewer.aspects.*.*(..))
			&& !within(@IgnoreExceptions *)
			&& !withincode(@IgnoreExceptions * *(..))
			&& handler(Throwable+) && args(throwable);

	before(Throwable throwable): npcException(throwable){
		logThrew(thisJoinPoint, throwable);
	}

	private final Logger logger;
	private final String logMsg;

	public ExceptionHandler() {
		super();

		logger = LoggerFactory.getLogger("exceptions");

		Resources resources = Resources.getInstance();
		logMsg = resources.getString("message.error-occurred");
	}

	void logThrew(JoinPoint joinPoint, Throwable t) {
		if (logger.isErrorEnabled())
			logger.error(MessageFormat.format(logMsg, joinPoint.getSourceLocation(), t.getLocalizedMessage()), t);
	}
}